package com.wenge.datagroup.storage.dataprocess;

import com.alibaba.fastjson.JSON;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.apache.log4j.PropertyConfigurator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

/**
 * @Author：WeiPengCheng
 * @Date：2022-07-27 10:55
 * @desc:单文档摘要
 **/
public class MultilingualAbstract {
    private static Logger logger = LoggerFactory.getLogger(MultilingualAbstract.class);
    private static String url;

    public static void setUrl(String url) {
        MultilingualAbstract.url = url;
    }

    public static void main(String[] args) {

        PropertyConfigurator.configure("config/log4j.properties");
        String content =
                "what";

        analysis("", "");
    }

    public static String analysis(String title, String article) {
        long start = System.currentTimeMillis();

        OkHttpClient httpClient = new OkHttpClient().newBuilder().build();
        MediaType mediaType = MediaType.parse("application/json");
        MultipartBody body = new MultipartBody.Builder().setType(MultipartBody.FORM)
                .addFormDataPart("id", "1")
                .addFormDataPart("title", title)
                .addFormDataPart("article", article)
                .build();

        Request request = new Request.Builder()
                .url(url)
                .method("POST", body)
                .addHeader("Content-Type", "application/json")
                .build();

        // 4 发起请求获取响应值
        try {
            Response response = httpClient.newCall(request).execute();
            if (response.code() == 200) {
                logger.info("自动摘要请求成功!!");
                String data = response.body().string();
                String string = JSON.parseObject(data).getString("Summary");
                logger.info("自动摘要结果:" + string);
                return string;

            } else {
                logger.info("多语言自动摘要接口请求异常! code:{}", response.code());
            }
        } catch (IOException e) {
            logger.error("多语言自动摘要接口请求异常!");
            e.printStackTrace();
        } finally {
            logger.info("多语言自动摘要接口处理耗时： " + (System.currentTimeMillis() - start) + " ms");
        }

        return null;
    }

}
